import 'package:blog/app/package_info.dart';
import 'package:blog/base/get/get_save_state_view.dart';
import 'package:blog/routes/routes.dart';
import 'package:blog/ui/page/complex_module/recommend_page/widget/banner_widget.dart';
import 'package:blog/ui/page/complex_module/recommend_page/widget/recommend_article_item.dart';
import 'package:blog/ui/page/complex_module/recommend_page/widget/recommend_wechat_public_widget.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import 'package:blog/util/web_util.dart';
import 'package:blog/widget/WideCard.dart';
import 'package:blog/widget/pull_smart_refresher.dart';
import 'package:blog/widget/ripple_widget.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'recommend_controller.dart';

/// @class : MainPage
/// @date : 2021/08/23
/// @name : CTGU_LLZ(404name)
/// @description :首頁的推荐
class MainPage extends GetSaveView<RecommendController> {
  const MainPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
          top: true,
          child: Column(
            children: [
              Expanded(
                  child: Container(
                      color: Colors.white,
                      child: RefreshWidget<RecommendController>(
                          child: ListView.builder(
                        padding: EdgeInsets.zero,
                        shrinkWrap: true,
                        itemCount: controller.projectData.length +
                            1 +
                            (controller.insertIndex == -1 ? 0 : 1),
                        itemBuilder: (BuildContext context, int index) {
                          ///将Banner装载到ListView中
                          if (index == 0) {
                            return  Container(
                              height: 200,
                              child: new Swiper(
                                itemBuilder: (BuildContext context, int index) {
                                  return InkWell(
                                      child: WideCard(label: controller.banner[index].title, imgStr: controller.banner[index].imagePath, isLocal: controller.banner[index].isAssets),
                                      onTap: (){
                                        WebUtil.toWebPageOther(title: controller.banner[index].title,link: controller.banner[index].url);
                                      }
                                  );
                                },
                                itemCount: controller.banner.length,
                                viewportFraction: 0.8,
                                scale: 0.9,
                                duration: 3000,
                              ),
                              padding: EdgeInsets.all(10),
                            );



                          } else if (index == controller.insertIndex) {
                            ///随机出现的公众号列表
                            return Obx(() => WechatPublicWidget(
                              isFirst: controller.isFirst,
                              wechatPublic: controller.showWechatPublic,
                              onChange: () => controller.notifyRandomPublic(),
                              showSwitch: controller.showSwitch.value,
                              showDelete: controller.showDelete.value,
                              onTap: () => controller.notifyButtonState(),
                            ));
                          } else {
                            ///计算当前显示的真实索引
                            var newIndex = index -
                                (index > controller.insertIndex &&
                                        controller.insertIndex != -1
                                    ? 2
                                    : 1);

                            ///item列表数据展示
                            return Material(
                              color: Colors.transparent,
                              child: Ripple(
                                  onTap: () => WebUtil.toWebPage(controller.projectData[newIndex] , onResult: (value){
                                    controller.projectData[newIndex].collect = value;
                                  }),
                                  child: MainArticleItem(
                                    item: controller.projectData[newIndex],
                                    index: newIndex,
                                  )),
                            );
                          }
                        },
                      ))))
            ],
          )),
    );
  }
}
